/**
 * @fileOverview  Contains various controller functions in the namespace "oa.ctrl" 
 * @author Gerd Wagner
 */
oa.ctrl = {
  rf: {},  // subnamespace for rendering forms
  /**
   *  Initialization is called when the HTML page has been loaded
   */
  initialize: function () {
    if (window.localStorage) { 
      oa.ctrl.loadData(); 
    } else {
      oa.learner = new Learner();
      lang = util.getUserLanguage();
      if (oa.supportedLanguages.indexOf(lang) === -1) { lang="en"; }
      oa.learner.setLanguage( lang);
    }
    oa.view.initializeUI();
  },
  /**
   *  Quit exercise
   */
  quit: function () {
    oa.currentLearningUnit = null;
    oa.currentExercise = null;
    oa.currentExerciseNumber = 0;
    oa.previousProblem = null;
    oa.currentProblem = null;
    oa.currentProblems = [];
    oa.currentProblemNumber = 0;
    oa.view.resetScreen();
  },
  /**
   *  Exit the application
   */
  exit: function () {
    if (localStorage && oa.learner.firstName) { oa.ctrl.saveData(); }
  },
  /**
   *  Load session data
   */
  loadData: function () {
    var gr=0, lang="";
    try {
      var fN = localStorage.getItem("learnerFirstName");
      if (!fN) { 
        oa.learner = new Learner();
        lang = util.getUserLanguage();
        if (oa.supportedLanguages.indexOf(lang) === -1) { lang="en"; }
        oa.learner.setLanguage( lang);
        oa.basicAdditionStatistics = BasicProblemStatistic.getInitialBasicProblemStatisics();
        oa.basicMultiplicationStatistics = BasicProblemStatistic.getInitialBasicProblemStatisics();
        oa.view.openDialogWindow(); 
      } else {
        gr = parseInt( localStorage.getItem("learnerGrade"));
        lang = localStorage.getItem("learnerLanguage");
        oa.learner = new Learner( fN, gr, lang);
        oa.basicAdditionStatistics = JSON.parse( localStorage.getItem("basicAdditionStatistics"));
        if (!oa.basicAdditionStatistics || oa.basicAdditionStatistics.length === 0) {
          oa.basicAdditionStatistics = BasicProblemStatistic.getInitialBasicProblemStatisics();
        }
        oa.basicMultiplicationStatistics = JSON.parse( localStorage.getItem("basicMultiplicationStatistics"));
        if (!oa.basicMultiplicationStatistics || oa.basicMultiplicationStatistics.length === 0) {
          oa.basicMultiplicationStatistics = BasicProblemStatistic.getInitialBasicProblemStatisics();
        }
      }
    } catch (e) {
      alert("Error when loading data\n" + e);        
    } 
  },
  /**
   * save session data
   */
  saveData: function () {
    try {
      localStorage.setItem("learnerFirstName", oa.learner.firstName);
      localStorage.setItem("learnerGrade", JSON.stringify( oa.learner.grade));
      localStorage.setItem("learnerLanguage", oa.learner.language);
      localStorage.setItem("basicAdditionStatistics", JSON.stringify( oa.basicAdditionStatistics));
      localStorage.setItem("basicMultiplicationStatistics", JSON.stringify( oa.basicMultiplicationStatistics));
    } catch (e) {
      if (e == QUOTA_EXCEEDED_ERR) {
        alert('Quota exceeded!');  // handle the error...?
      } else {
        alert("Error when writing to Local Storage\n" + e);        
      } 
    };
  },
  /**
   * reset persistent data by clearing local storage
   */
  resetData: function () {
    if (window.localStorage) {
      try {
        localStorage.clear();
      } catch (e) {
        alert("Error while trying to clear local storage:\n" + e.message);
      }      
    }
    oa.view.clearLearnerData();
    // close dialog window
    document.body.classList.toggle("overlaid");
  }
};
window.addEventListener("load", oa.ctrl.initialize, false);
window.onbeforeunload = oa.ctrl.exit;
